package com.feishi.core.util.http;

import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class Search {
    public static void main(String[] args) throws Exception {
        String href="http://38.103.161.132/forum/forum-383-1.html";
        while (href!=null){
            System.out.println(href);
            href=scan(href,"383");
        }
    }

    public static String scan(String href,String id) throws IOException {
        Connection con = Jsoup.connect(href);
//        con.header("Cookie","_ga=GA1.1.1977654360.1556387044; cdb2_oldtopics=D10449244D10422753D8953663D9896350D; cdb2_fid383=1558114413; cdb2_sid=TYBSyM");

        Document doc = con.get();
        Elements eles = doc.select("#forum_"+id);
        Elements newsHeadlines =eles.get(eles.size()-1).select("tbody th");
        for (Element headline : newsHeadlines) {
            if(headline.select("span a").size()==0){continue;};
            Element a = headline.select("span a").get(0);
            System.out.println(String.format("%s\t%s",a.wholeText(), a.absUrl("href")));
           boolean status=true;
           int count=10;
           while (status){
               try {
                   String fileName="E:\\a\\"+id+"\\"+a.wholeText().replaceAll("[\\\\/:*?\"<>|]","_")+".txt";
                   getFile(fileName, a.absUrl("href"));
                   status=false;
               }catch (Exception e){
                   e.printStackTrace();
                   if(count--<0)break;
               }
           }

        }
        Elements next = doc.select(".pages_btns .pages .next");
        if(next.size()>0){
            return next.get(0).absUrl("href");
        }
        return null;
    }

    private static void getFile(String fileName, String href) throws IOException {
        href=Jsoup.connect(href).get().select(".mainbox.viewthread .postinfo a").get(0).absUrl("href");

        Connection con = Jsoup.connect(href);
        Document doc = con.get();
        File file=new File(fileName);
        FileOutputStream outputStream=new FileOutputStream(file);
        ;
        for (Element headline : doc.select(".mainbox.viewthread .postcontent .noSelect.t_msgfont")) {
            outputStream.write(headline.wholeText().getBytes());
            outputStream.flush();
        }
        outputStream.close();


    }
}
